//
//  TitleNavigationBar.m
//  mcrmSAAS
//
//  Created by wjxfhxy on 15/7/10.
//  Copyright (c) 2015年 GuangZhouXuanWu. All rights reserved.
//

#import "TitleNavigationBar.h"

@interface TitleNavigationBar ()<XUIButtonDelegate>
{
    UIScrollView *_scrollView;
    
    NSMutableArray *_controls;
    
    UIView *_line;
}

@end

@implementation TitleNavigationBar

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    
    _scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, self.width, self.height)];
    _scrollView.contentInset = UIEdgeInsetsMake(0, 20, 0, 20);
    _scrollView.showsHorizontalScrollIndicator = NO;
    
    [self addSubview:_scrollView];
    
    CAGradientLayer * left = [CAGradientLayer new];
    left.frame = CGRectMake(0, 0, 20, self.height);
    left.colors = @[(id)[UIColor whiteColor].CGColor,
                    (id)[UIColor colorWithRed:1 green:1 blue:1 alpha:0.9].CGColor,
                    (id)[UIColor colorWithRed:1 green:1 blue:1 alpha:0.0].CGColor];
    left.startPoint = CGPointMake(0, 0.5);
    left.endPoint = CGPointMake(1, 0.5);
  //  left.locations = @[@(0),@(.6),@(1)];
    [self.layer addSublayer:left];
    
    CAGradientLayer * right = [CAGradientLayer new];
    right.frame = CGRectMake(self.width - 20, 0, 20, self.height);
    right.colors = @[(id)[UIColor colorWithRed:1 green:1 blue:1 alpha:0.0].CGColor,
                    (id)[UIColor colorWithRed:1 green:1 blue:1 alpha:0.1].CGColor,
                    (id)[UIColor whiteColor].CGColor];
    right.startPoint = CGPointMake(0, 0.5);
    right.endPoint = CGPointMake(1, 0.5);
    [self.layer addSublayer:right];
    
    _controls = [NSMutableArray new];
    
    _line = [[UIView alloc]initWithFrame:CGRectMake(0, self.height - 0.5, self.width, 0.5)];
    _line.backgroundColor = [UIColor colorWithHex:mcrmSAASUISkinColorTextStyleLineColor];
    [self addSubview:_line];
    
    return self;
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    _scrollView.frame = self.bounds;
    
    _line.frame = CGRectMake(0, self.height - 0.5, self.width, 0.5);
}

- (void)addTitle:(NSString*)title
{
    CGRect frame = CGRectMake(0, 0, [title stringSizeWithFont:[UIFont systemFontOfSize:15]].width + 20, _scrollView.height);
    if(_controls.count > 0)
    {
        frame.origin.x = ((UIView*)_controls[_controls.count - 1]).right;
        
        UIView *arrow = [[UIImageView alloc]initWithFrame:CGRectMake(frame.origin.x, 0, 16, _scrollView.height)];
        UIImageView *imageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"img_titlenavarrow.png"]];
        [arrow addSubview:imageView];
        imageView.center = CGPointMake(arrow.width / 2, arrow.height / 2);
        
        [_scrollView addSubview:arrow];
        [_controls addObject:arrow];
        
        frame.origin.x += arrow.width;
    }
    
    XUIButton *btn = [XUIButton buttonWithStyleType:XUIButtonStyleTypeCustom delegate:self];
 //   btn.backgroundColor = [UIColor orangeColor];
    btn.frame = frame;
    [btn SetFont:[UIFont systemFontOfSize:15]];
    [btn SetTitle:title];
    [btn SetTitleColor:[UIColor colorWithHex:mcrmSAASUISkinColorTextStyleLightBlueColor]];
    
    [_scrollView addSubview:btn];
    [_controls addObject:btn];
    
    _scrollView.contentSize = CGSizeMake(btn.right, 0);
    
    CGFloat offsetX = 0;
    if(_scrollView.contentSize.width > _scrollView.width - 20 - 20)
    {
        offsetX = _scrollView.contentSize.width - (_scrollView.width - 20 );
        [_scrollView setContentOffset:CGPointMake(offsetX, 0) animated:YES];
    }
}

- (void)removeTitleFromIndex:(NSUInteger)index
{
    NSUInteger i = index;
    i = i == 0 ? i : i * 2;
    NSArray *ar = [_controls objectsAtIndexes:[[NSIndexSet alloc]initWithIndexesInRange:NSMakeRange(i + 1, _controls.count - (i + 1))]];
    [ar makeObjectsPerformSelector:@selector(removeFromSuperview)];
    _scrollView.contentSize = CGSizeMake(((UIView*)_controls[_controls.count - 1]).right, 0);
    [_controls removeObjectsInArray:ar];
}

- (void)clickedUpInsideOfButton:(XUIButton *)btn
{
    NSUInteger i = [_controls indexOfObject:btn];
    i = i == 0 ? i : i / 2;
    [_delegate titleNavigationBar:self selectedAtIndex:i];
}

@end
